package leetcode_161_180;

import utils.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 中序遍历的二叉搜索树（BST）的迭代器
 */
public class BSTIterator {
    int index=0;

    List<Integer>list;

    public BSTIterator(TreeNode root) {
        list=new ArrayList<>();
        function(root);
    }
    void function(TreeNode root){
        if(root==null)
            return;
        function(root.left);
        list.add(root.val);
        function(root.right);
    }

    public int next() {
        return list.get(index++);
    }

    public boolean hasNext() {
        return index != list.size();
    }
}
